﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Notoric.Gui.Converters
{
    /// <summary>
    /// Converts a length in device independent units to centimeters and formats the result
    /// as a string where the number has two digits after the decimal point and a " cm" suffix.
    /// </summary>
    [ValueConversion(typeof(double), typeof(string))]
    class LengthFormatConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <inheritdoc/>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double val = (double)value;
            if (Double.IsNaN(val))
                return Double.NaN;

            return String.Format("{0:F} cm", val / cmToPx);
        }

        /// <inheritdoc/>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// 1cm = (96 / 2.54)px
        /// </summary>
        private const double cmToPx = 96 / 2.54;
    }
}
